<template>
  <div class="backBox">
    <div class="loginBox">
      <div class="logo">
        <!-- <img src="../../assets/logo/logo.png" alt="" /> -->
        <span>与你同行</span>
      </div>
      <div class="loginText">
        <h1>登录</h1>
        <p>请先登录才能进行其他操作</p>
      </div>
      <el-form
        :model="form"
        label-position="top"
        label-width="auto"
        style="width: 420px; margin-top: 20px"
        :rules="rules"
      >
        <el-form-item label="账号" prop="account">
          <el-input :prefix-icon="User" v-model="form.account" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input :prefix-icon="Lock" type="password" v-model="form.password" />
        </el-form-item>
        <el-form-item>
          <!-- <el-checkbox-group v-model="form.type">
            <el-checkbox value="1" name="type"> 记住我 </el-checkbox>
          </el-checkbox-group>
          <p
            style="margin-left: 294px; color: gray"
            @click="$router.push('/forget')"
          >
            忘记密码
          </p> -->
        </el-form-item>

        <el-form-item>
          <el-button @click="login" style="width: 420px" type="primary">登录</el-button>
        </el-form-item>
      </el-form>

      <!-- <div class="bottomText">
        <span>没有账号？</span>
        <span @click="$router.push('/register')">去注册</span>
      </div> -->
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { User, Lock } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import { loginInAPI } from "../../apis/landing";
import { useUserStore } from "../../stores/userStore";
import CryptoJS from 'crypto-js'

const router = useRouter();
const userStore = useUserStore();

function encryptPassword(password:string) {
  var key = CryptoJS.enc.Utf8.parse("1234567890123456"); // 加密密钥
  var iv = CryptoJS.enc.Utf8.parse("1234567890123456"); // 向量
  var encrypted = CryptoJS.AES.encrypt(password, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  });
  return encrypted.toString();
}

const form = ref({
  account: "",
  password: "",
  type: [],
});

const rules = reactive({
  account: [
    {
      required: true,
      message: "不能为空",
      trigger: "blur",
    },
  ],
  password: [
    {
      required: true,
      message: "不能为空",
      trigger: "blur",
    },
  ],
});

const login = async () => {

  console.log(encryptPassword(form.value.password))
  const res = await loginInAPI(form.value.account, encryptPassword(form.value.password));

  if (res.data.data.success) {
    ElMessage.success("登录成功");

    userStore.setUser(res.data.roles, res.data.token);

    setTimeout(() => {
      router.push("/");
    }, 2000);
  } else {
    ElMessage.error(res.data.data.message);
  }
};
</script>

<style lang="scss" scoped>
.backBox {
  background: #f5f7fd;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;

  img {
    width: 60px;
  }

  .logo {
    display: flex;
    justify-content: center;
    align-items: center;

    span {
      margin-left: 20px;
      font-weight: bold;
      font-size: 30px;
      color: #409eff;
    }
  }

  .loginText {
    font-size: 12px;

    p {
      margin-top: 10px;
      color: #409eff;
    }
  }

  .loginBox {
    display: flex;
    flex-direction: column;
    background: white;
    padding: 40px;
  }

  .bottomText {
    display: flex;
    justify-content: center;

    span:nth-child(2) {
      cursor: pointer;
      color: #409eff;
    }
  }
}
</style>
